package com.autumn.spring.boot.context.event;

import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;

import com.autumn.util.EnvironmentConstants;
import com.autumn.util.Version;

/**
 * Banner 上下文应用事件
 * 
 * @author 老码农
 *
 *         2018-06-27 14:29:25
 */
@Order(LoggingApplicationListener.DEFAULT_ORDER)
public class AutumnSpringApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

	private static final Log logger = LogFactory.getLog(AutumnSpringApplicationListener.class);
	
	/**
	 * autumn Logo
	 */
	public static final String AUTUMN_LOGO = 
			  "         ◆              ◆                       ◆   ◆◆◆◆◆◆◆◆◆◆◆   ◆                       ◆   ◆                           ◆   ◆                  ◆\n"
			+ "        ◆ ◆            ◆                       ◆                     ◆                  ◆                       ◆   ◆◆                      ◆◆  ◆◆                ◆\n"
			+ "       ◆     ◆           ◆                       ◆                     ◆                  ◆                       ◆   ◆  ◆                  ◆  ◆  ◆  ◆              ◆\n"
			+ "      ◆         ◆         ◆                       ◆                     ◆                  ◆                       ◆   ◆    ◆              ◆    ◆  ◆     ◆           ◆\n"
			+ "     ◆◆◆◆◆◆        ◆                       ◆                     ◆                  ◆                       ◆   ◆      ◆          ◆      ◆  ◆        ◆        ◆\n"
			+ "    ◆                 ◆      ◆                        ◆                     ◆                  ◆                       ◆   ◆        ◆       ◆       ◆  ◆          ◆      ◆\n"
			+ "   ◆                     ◆     ◆                        ◆                     ◆                  ◆                       ◆   ◆         ◆    ◆         ◆  ◆             ◆   ◆\n"
			+ "  ◆                         ◆    ◆                       ◆                     ◆                  ◆                       ◆   ◆          ◆  ◆           ◆  ◆               ◆ ◆\n"
			+ " ◆                             ◆   ◆◆◆◆◆◆◆◆◆                     ◆                  ◆◆◆◆◆◆◆◆◆    ◆            ◆             ◆   ◆                  ◆\n";

	private Banner.Mode getBannerMode(SpringApplication application) {
		try {
			Field field = SpringApplication.class.getDeclaredField("bannerMode");
			try {
				field.setAccessible(true);
				Banner.Mode mode = (Mode) field.get(application);
				return mode;
			} catch (IllegalArgumentException | IllegalAccessException e) {
				return Banner.Mode.OFF;
			}
		} catch (NoSuchFieldException | SecurityException e) {
			return Banner.Mode.OFF;
		}
	}

	@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		Banner.Mode mode = this.getBannerMode(event.getSpringApplication());
		if (mode == Banner.Mode.OFF) {
			return;
		}		
		String bannerText = this.buildBannerText();
		if (mode == Mode.CONSOLE) {
			System.out.print(bannerText);
		} else if (mode == Mode.LOG) {
			logger.info(bannerText);
		}
	}

	private String buildBannerText() {
		StringBuilder bannerTextBuilder = new StringBuilder();
		bannerTextBuilder.append(EnvironmentConstants.LINE_SEPARATOR).append(AUTUMN_LOGO)
				.append(" :: Autumn ::        (v").append(Version.getVersion()).append(")")
				.append(EnvironmentConstants.LINE_SEPARATOR);
		return bannerTextBuilder.toString();
	}
}
